

<title>分类管理</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>应用</cite></a>
    <a><cite>分类管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <!-- <div class="layui-card-header layuiadmin-card-header-auto">
      <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加</button>
    </div> -->
    <div class="layui-card-body">    
      <table id="LAY-app-content-tags" lay-filter="LAY-app-content-tags"></table>  
      <script type="text/html" id="layuiadmin-app-cont-tagsbar">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
layui.use('contlist', layui.factory('contlist')).use(['admin', 'contlist', 'table'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;
  
  var active = {
    add: function(){
      admin.popup({
        title: '添加分类'
        ,area: ['500px', '230px']
        ,id: 'LAY-popup-content-tags'
        ,success: function(layero, index){
          view(this.id).render('app/content/tagsform').done(function(){
            form.render(null, 'layuiadmin-form-tags');
            
            //动态下拉框
              $.post(
              	  	"category/findAllByid.do",
              	  	function(res){
              	  		//console.log(res);
              	  		$(res).each(function(){
              	  			//创建一个下拉选项
              	  			var option = $('<option value="'+this.id+'">'+this.name+'</option>');
              	  			//将下拉选项追加到下拉框中
              	  			$("#cate1").append(option);
              	  		});
              	  		form.render('select'); //刷新select选择框渲染
              	  	},'json'
              	  );
            //监听提交
            form.on('submit(layuiadmin-app-tags-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              //$.ajax({});
              $.post(
      	    		"category/addOredit.do",
      	    		data.field,
      	    		function(res){
      	    			if(res>0){
      	    				layer.msg("添加成功");
      	    				window.setTimeout(function() {
      	    					location.reload();
      							return true;
      						}, 1000);
      	    			}else{
      	    				layer.msg("添加失败");
      	    				window.setTimeout(function() {
      	    					location.reload();
      							return false;
      						}, 1000);
      	    			}
      	    			console.log(res);
      	    			
      	    		},'json'
      	      ); 
              layui.table.reload('LAY-app-content-tags'); //重载表格
              layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  }  
  $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>